package com.sgcc.operate.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.sgcc.common.datascope.annotation.DataScope;
import com.sgcc.operate.domain.OActiApply;
import com.sgcc.operate.domain.OPrizeCost;
import com.sgcc.operate.domain.OSchemeForm;
import com.sgcc.operate.service.IOActiApplyService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sgcc.common.log.annotation.Log;
import com.sgcc.common.log.enums.BusinessType;
import com.sgcc.common.security.annotation.PreAuthorize;
import com.sgcc.operate.domain.OActiInfo;
import com.sgcc.operate.service.IOActiInfoService;
import com.sgcc.common.core.web.controller.BaseController;
import com.sgcc.common.core.web.domain.AjaxResult;
import com.sgcc.common.core.utils.poi.ExcelUtil;
import com.sgcc.common.core.web.page.TableDataInfo;

/**
 * 活动信息Controller
 *
 * @author sgcc
 * @date 2021-03-23
 */
@RestController
@RequestMapping("/actiinfo")
public class OActiInfoController extends BaseController {
    @Autowired
    private IOActiInfoService oActiInfoService;
    @Autowired
    private IOActiApplyService oActiApplyService;

    /**
     * 查询活动信息列表
     */
    //@PreAuthorize(hasPermi = "operate:info:list")
    @GetMapping("/list")
    public TableDataInfo list(OActiInfo oActiInfo) {
        startPage();
        List<OActiInfo> list = oActiInfoService.selectOActiInfoList(oActiInfo);
        return getDataTable(list);
    }

    /**
     * 查询活动信息列表
     */
    //@PreAuthorize(hasPermi = "operate:info:list")
    @GetMapping("/rule/list")
    public TableDataInfo ruleLlist(OActiInfo oActiInfo) {
        startPage();
        List<OActiInfo> list = oActiInfoService.selectOActiInfoList(oActiInfo);
        return getDataTable(list);
    }

    /**
     * 导出活动信息列表
     */
    //@PreAuthorize(hasPermi = "operate:info:export")
    @Log(title = "活动信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OActiInfo oActiInfo) throws IOException {
        List<OActiInfo> list = oActiInfoService.selectOActiInfoList(oActiInfo);
        ExcelUtil<OActiInfo> util = new ExcelUtil<OActiInfo>(OActiInfo.class);
        util.exportExcel(response, list, "info");
    }

    /**
     * 获取活动信息详细信息
     */
    //@PreAuthorize(hasPermi = "operate:info:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(oActiInfoService.selectOActiInfoById(id));
    }

    /**
     * 新增活动信息
     */
    //@PreAuthorize(hasPermi = "operate:info:add")
    @Log(title = "活动信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OActiInfo oActiInfo) {
        return toAjax(oActiInfoService.insertOActiInfo(oActiInfo));
    }

    /**
     * 修改活动信息
     */
    //@PreAuthorize(hasPermi = "operate:info:edit")
    @Log(title = "活动信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OActiInfo oActiInfo) {
        return toAjax(oActiInfoService.updateOActiInfo(oActiInfo));
    }

    /**
     * 删除活动信息
     */
    //@PreAuthorize(hasPermi = "operate:info:remove")
    @Log(title = "活动信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(oActiInfoService.deleteOActiInfoByIds(ids));
    }


    /**
     *
     */
    @PostMapping(value = "oActiRuleList")
    public TableDataInfo oActiRuleList(OActiInfo oActiInfo) {
        startPage();
        List<OActiInfo> list = oActiInfoService.oActiRuleList(oActiInfo);
        return getDataTable(list);
    }


    /**
     * 活动管理列表
     */
    @GetMapping(value = "actiManageListData")
    @DataScope(deptAlias="c")
    public TableDataInfo actiManageListData(OActiInfo oActiInfo) {
        startPage();
        List<OActiInfo> list = oActiInfoService.actiManageList(oActiInfo);
        return getDataTable(list);
    }


    /**
     * 获取活动信息详细信息
     */
    //@PreAuthorize(hasPermi = "operate:info:query")
    @GetMapping(value = "/actiMonitorPageInfo/{id}")
    public AjaxResult actiMonitorPageInfo(@PathVariable("id") String id) {

        List<Map<String, Integer>> prizeDetails = oActiInfoService.prizeDetails(id);

        return AjaxResult.success(prizeDetails);
    }

    /**
     * 启用或者作废，运营方案制定
     */
    @PostMapping(value = "beginOrEnd/{status}/{ids}")
    public AjaxResult deleteAll(@PathVariable("ids")String ids,@PathVariable("status")String status) {
        String idArray[] =ids.split(",");
        for(String id : idArray){
            OActiInfo oActiInfo = oActiInfoService.selectOActiInfoById(id);
            if ("02".equals(oActiInfo.getActistatus())&&"02".equals(status)) {
                return AjaxResult.error("该活动已开启");
            }
//			if ("02".equals(oSchemeForm.getSchemestatus())&&"03".equals(schemestatus)) {
//				if (oSchemeForm.getOindiContentList().size() > 0) {
//					j.setMsg("运营指标已被运营方案绑定不能作废！");
//					return j;
//				}
//			}
            OActiInfo update=new OActiInfo();
            update.setActistatus(status);
            update.setId(id);
            oActiInfoService.update(update);
        }
        return AjaxResult.success("操作成功");
    }


    /**
     * 活动管理 -- 预览
     * @return
     */
    @GetMapping(value = "preview")
    public AjaxResult preview(String tempId, String actiId) {
        if (StringUtils.isBlank(actiId) || StringUtils.isBlank(tempId)) {
            return AjaxResult.error("参数不能为空");
        }
        OActiApply apply = oActiApplyService.queryApplyActi(tempId, actiId);
        return AjaxResult.success(apply);
    }

    /**
     * 获取活动的奖品数目
     * @param actiInfo
     * @return
     */
    @GetMapping(value = "getPrizeNum")
    public AjaxResult getPrizeNum(OActiInfo actiInfo) {
        OActiInfo oActiApply = oActiInfoService.selectOActiInfoById(actiInfo.getId());
        List<OPrizeCost> list = new ArrayList<OPrizeCost>();
        for (OPrizeCost prizeCost : oActiApply.getOprizeCostList()) {
            if (!"99".equals(prizeCost.getPrizetype())) {
                list.add(prizeCost);
            }
        }
        return AjaxResult.success(list);
    }
}
